package com.ogawa.projectcommon.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ogawa.base.utils.ScreenUtils;
import com.ogawa.projectcommon.R;

import java.util.ArrayList;
import java.util.List;




//切换提示开关
public class TouchSwitchView extends FrameLayout {
    private int imageViewSize = 0;
    private int titleSize = 0;
    private TextView title;
    private ImageView imageView;
    private int imagePreViewRes;
    private String titlePreViewStr;

    private List<TouchSwitchViewBean> touchSwitchViewBeans
            = new ArrayList<TouchSwitchViewBean>();
    private int curStatusIndex = 0;
    public TouchSwitchView(@NonNull Context context) {
        super(context);
        init(null);
    }


    public TouchSwitchView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }
    public int getCurStatusIndex() {
        return curStatusIndex;
    }

    public void setCurStatusIndex(int curStatusIndex) {
        this.curStatusIndex = curStatusIndex;
    }


    void init(AttributeSet attrs){
        imageViewSize = ScreenUtils.dip2px(getContext(), 45);
        titleSize = 18;
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.touch_switch_view);
        if (attrs != null) {
            try {
                imageViewSize = a.getDimensionPixelSize(R.styleable.touch_switch_view_touch_switch_view_iamge_view_size,
                        imageViewSize);
                titleSize = a.getDimensionPixelSize(R.styleable.touch_switch_view_touch_switch_view_title_size,
                        titleSize);
                imagePreViewRes = a.getResourceId(R.styleable.touch_switch_view_touch_switch_view_image,
                        R.drawable.img_advance_s);
                titlePreViewStr = a.getString(R.styleable.touch_switch_view_touch_switch_view_title);
                if (TextUtils.isEmpty(titlePreViewStr)){
                    titlePreViewStr = "title";
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                {
                    a.recycle();
                }
            }
        }
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_touch_switch_view,null);
        imageView = view.findViewById(R.id.imageIv);
//        LinearLayout.LayoutParams imagelayoutParams = new
//                LinearLayout.LayoutParams(imageViewSize,imageViewSize);
//        imagelayoutParams.gravity = Gravity.CENTER;
//        imageView.setLayoutParams(imagelayoutParams);
        imageView.setImageResource(imagePreViewRes);

        title = view.findViewById(R.id.titleTv);
//        LinearLayout.LayoutParams titlelayoutParams = new
//                LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//        titlelayoutParams.topMargin = ScreenUtils.dip2px(getContext(), 15);
//        titlelayoutParams.gravity = Gravity.CENTER;
//        title.setLayoutParams(titlelayoutParams);
//        title.setTextSize(titleSize);
        title.setText(titlePreViewStr);

//        addView(imageView);
//        addView(title);

        addView(view);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP){
            curStatusIndex++;
            if (curStatusIndex >= touchSwitchViewBeans.size()){
                curStatusIndex = 0;
            }
            setRes(curStatusIndex);
        }
        return super.onTouchEvent(event);
    }

    public void setTouchSwitchViewBeans(List<TouchSwitchViewBean> touchSwitchViewBeans) {
        this.touchSwitchViewBeans = touchSwitchViewBeans;
        curStatusIndex = 0;
        setRes(curStatusIndex);
    }


    private void setRes(int index){
        try {
            if (touchSwitchViewBeans != null && touchSwitchViewBeans.get(index)!= null){
                TouchSwitchViewBean touchSwitchViewBean = touchSwitchViewBeans.get(index);
                imageView.setImageResource(touchSwitchViewBean.getImageRes());
                title.setText(touchSwitchViewBean.title);
                title.setTextColor(touchSwitchViewBean.titleColorRes);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public class TouchSwitchViewBean{
        private int imageRes;
        private String title;
        private int titleColorRes;

        public TouchSwitchViewBean(int imageRes, String title, int titleColorRes) {
            this.imageRes = imageRes;
            this.title = title;
            this.titleColorRes = titleColorRes;
        }

        //
        public int getImageRes() {
            return imageRes;
        }

        public void setImageRes(int imageRes) {
            this.imageRes = imageRes;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getTitleColorRes() {
            return titleColorRes;
        }

        public void setTitleColorRes(int titleColorRes) {
            this.titleColorRes = titleColorRes;
        }
    }
}
